import React, { useState, useEffect } from 'react';
import Web3 from 'web3';
import { useParams, Link } from 'react-router-dom';
import CourseRecordListABI from "../contract/ABIs/CourseList.json"; // 课程记录列表合约 ABI
import ReviewerListABI from '../contract/ABIs/ReviewerList.json'; // 审核员列表合约 ABI
import ReviewerABI from '../contract/ABIs/Reviewer.json'; // 审核员合约 ABI

import CourseRecordListAddress from "../contract/ADDRESSes/CourseListAddress.js" // 课程记录列表合约地址
import ReviewerListAddress from '../contract/ADDRESSes/ReviewerListAddress.js'; // 审核员列表合约地址
import { set } from 'date-fns';
import ReviewerNavBar from './nav/ReviewerNavBar.js';
// import '../static/indexcss/aacsss.css';
import gb from '../static/bootstrap/images/gb.svg';
import us from '../static/bootstrap/images/us.svg';
import user3 from '../static/bootstrap/images/user3.png';
import Head from './nav/Head.js';


const Reviewer_doCourseRecord = () => {
    const [web3, setWeb3] = useState(null);
    const { address } = useParams();
    const [contractCourseRecordList, setcontractCourseRecordList] = useState(null);
    //const [contractAdmin, setcontractAdmin] = useState(null);
    //const [contractUserList, setContractUserList] = useState(null);
    const [contractReviewerList, setcontractReviewerList] = useState(null);
    const [contractReviewer, setcontractReviewer] = useState(null);

    const [userAddress, setUserAddress] = useState('');
    const [courseNumber, setCourseNumber] = useState('');
    const [status, setStatus] = useState('');
    const [courseScore, setCourseScore] = useState('');
    const [examScore, setExamScore] = useState('');

    const [isAdmin, setIsAdmin] = useState(false);
    useEffect(() => {
        async function init() {
            // 连接到以太坊网络
            if (window.ethereum) {
                const web3Instance = new Web3(window.ethereum);
                try {
                    // 请求用户授权
                    await window.ethereum.enable();
                    setWeb3(web3Instance);

                    // 创建课程记录合约实例
                    const courseRecordListInstance = new web3Instance.eth.Contract(
                        CourseRecordListABI,
                        CourseRecordListAddress
                    );
                    setcontractCourseRecordList(courseRecordListInstance)

                    const reviewerInstance = new web3Instance.eth.Contract(
                        ReviewerABI,
                        address
                    );
                    setcontractReviewer(reviewerInstance);

                    const reviewerListInstance = new web3Instance.eth.Contract(
                        ReviewerListABI,
                        ReviewerListAddress
                    );
                    setcontractReviewerList(reviewerListInstance);

                    // 获取当前用户的以太坊账户地址
                    const accounts = await web3Instance.eth.getAccounts();
                    const currentAddress = accounts[0];
                    // 获取管理员合约的 owner 地址
                    const ownerAddress = await reviewerInstance.methods.getOwner().call();
                    // 检查当前账户是否是管理员本人
                    setIsAdmin(currentAddress === ownerAddress);
                } catch (error) {
                    console.error(error);
                }
            } else {
                console.error('请安装以太坊浏览器插件，如 MetaMask');
            }
        }
        init();
    }, [address]);
    const doCourserecord = async () => {
        if (contractCourseRecordList) {
            try {
                // 获取当前用户的以太坊账户地址
                const accounts = await web3.eth.getAccounts();
                const fromAddress = accounts[0];
                console.log("fromAddress:", fromAddress, "Input:", userAddress, courseNumber, courseScore, examScore);
                await contractCourseRecordList.methods.doCourseRecord(userAddress, courseNumber, courseScore, examScore).send({ from: fromAddress });
                alert('录入成绩成功！');
            } catch (error) {
                console.error(error);
                alert('录入成绩失败！');
            }
        };
    };
    return (
        <div>
            {/* 如果当前账户不是管理员，则显示提示信息 */}
            {!isAdmin && (<div>您没有权限访问此页面。</div>)}

            {/* 管理员界面 */}
            {isAdmin && (
                <div className="page-wrapper" style={{
                    backgroundColor: '#FFFFFF'
                }}>
                    <div className="main-container">
                        <ReviewerNavBar address={address} />

                        <div className="app-container">
                            <Head></Head>
                            <div className="app-body">
                                <div className="container-fluid">

                                    <div>
                                        <main className="page hire-me-page">
                                            <section className="portfolio-block hire-me">

                                                <div className="container">
                                                    <div className="border rounded border-0 shadow-lg p-3 p-md-5"
                                                        data-bs-theme="light"
                                                        style={{
                                                            backgroundColor: 'rgba(36, 36, 41, 0.6)'
                                                        }}
                                                    >


                                                        <div class="mb-3">
                                                            <h1>录入学生成绩</h1>
                                                            <div>


                                                                <div className="mb-3">
                                                                    <label className="form-label" htmlFor="name">用户地址:</label>
                                                                    <input className="form-control item" id="name" type="text" placeholder="用户地址" value={userAddress} onChange={(e) => setUserAddress(e.target.value)} />
                                                                </div>
                                                                <br />
                                                                <div className="mb-3">
                                                                    <label className="form-label" htmlFor="name">课程编号:</label>
                                                                    <input className="form-control item" id="name" type="text" placeholder="课程编号" value={courseNumber} onChange={(e) => setCourseNumber(e.target.value)} />
                                                                </div>
                                                                {/* <div className="mb-3">
                                                <label className="form-label" htmlFor="name">课程状态:</label> */}
                                                                {/* <input className="form-control item" id="name" type="text" placeholder="状态" value={status} onChange={(e) => setStatus(e.target.value)} /> */}
                                                                {/* <select className="form-select" value={status} onChange={(e) => setStatus(e.target.value)}>
                                                    <option value="0">未完成</option>
                                                    <option value="1">通过</option>
                                                    <option value="2">未通过</option>
                                                </select>
                                            </div> */}
                                                                <br />
                                                                <div className="mb-3">
                                                                    <label className="form-label" htmlFor="name">平时成绩:</label>
                                                                    <input className="form-control item" id="name" type="text" placeholder="平时成绩" value={courseScore} onChange={(e) => setCourseScore(e.target.value)} />
                                                                </div>
                                                                <br />
                                                                <div className="mb-3">
                                                                    <label className="form-label" htmlFor="name">考试成绩:</label>
                                                                    <input className="form-control item" id="name" type="text" placeholder="考试成绩" value={examScore} onChange={(e) => setExamScore(e.target.value)} />
                                                                </div>
                                                                <br />
                                                                <div className="mb-3 mt-4">
                                                                    <button className="btn btn-primary btn-lg d-block w-100" onClick={doCourserecord}>录入</button>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

                                            </section>
                                        </main>
                                    </div>


                                </div>
                            </div>
                            <div className="app-footer">
                                <span>
                                    © Bootstrap Gallery 2023
                                </span>
                            </div>
                        </div>
                    </div>

                </div>
            )};
        </div>

    );
};
export default Reviewer_doCourseRecord;